package cn.gz.demo.ui;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;

import androidx.databinding.DataBindingUtil;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.File;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Nullable;

import cn.gz.demo.ExportBinding;
import cn.gz.demo.R;
import cn.gz.demo.bean.ExcelBean;
import cn.gz.demo.ui.base.BaseActivity;
import cn.gz.demo.utils.ExcelUtils;
import cn.gz.demo.utils.FileUtils;
import cn.gz.demo.utils.LogUtil;
import cn.gz.demo.utils.ToastUtil;
import jxl.write.WritableCellFormat;
import jxl.write.WriteException;

/**
 * Created by Lyz on 2017/3/8.
 */
public class ExportActivity extends BaseActivity {
    private ExportBinding binding;
    public static final int REQUEST_CODE = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_export);

        binding.btnExport.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                export();
            }
        });
        binding.btnImport.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                intent.setType("*/*");
                startActivityForResult(intent, REQUEST_CODE);

//                List<ExcelBean> list = ExcelUtils.getInstance().readExcel(new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/ExportExcel", "工作表.xls"));
//                binding.content.setText(new Gson().toJson(list));
            }
        });
    }

    private void export() {
        ExcelUtils excelUtils = ExcelUtils.getInstance().create(Environment.getExternalStorageDirectory().getAbsolutePath() + "/ExportExcel", "工作表");
        try {
            WritableCellFormat titleFormat = new WritableCellFormat();
            WritableCellFormat dataFormat = new WritableCellFormat();
            String[] titles = new String[]{"姓名", "年龄", "省", "市", "区"};
            String[] titleKey = new String[]{"name", "age", "province", "city", "area"};
            List<Object> fileList = new ArrayList<>();
            fileList.add(new ExcelBean("小明", "20", "广东省", "广州", "天河"));
            fileList.add(new ExcelBean("小红", "18", "广东省", "广州", "天河"));
            fileList.add(new ExcelBean("小红", "18", "广东省", "广州", "天河"));
            fileList.add(new ExcelBean("小红", "18", "广东省", "广州", "天河"));

            final Gson gson = new Gson();
            final Type listType = new TypeToken<List<Map<String, Object>>>() {
            }.getType();
            List<Map<String, Object>> exportList = gson.fromJson(gson.toJson(fileList), listType);
            excelUtils.createSheetSetTitle("工作表", titles, titleFormat).fillData(titleKey, exportList, dataFormat).close();
            ToastUtil.showToast("表格已存入: " + Environment.getExternalStorageDirectory().getAbsolutePath() + "/ExportExcel");
        } catch (WriteException e) {
            e.printStackTrace();
        }
    }

    // 获取文件的真实路径
    @Override
    protected void onActivityResult(final int requestCode, final int resultCode, @Nullable final Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data == null) {
            // 用户未选择任何文件，直接返回
            return;
        }
        Uri uri = data.getData(); // 获取用户选择文件的URI
        LogUtil.showLog("File Uri: " + uri.toString());
        // Get the path
        String path = FileUtils.getPath(this, uri);
        LogUtil.showLog("File Path: " + path);
        List<ExcelBean> list = ExcelUtils.getInstance().readExcel(new File(path));
        binding.content.setText(new Gson().toJson(list));
    }
}
